package com.bq.gametrade.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.ui.dialog.ChangeNoteDialog;
import com.bq.gametrade.utils.Base64Util;
import com.bq.gametrade.utils.SharedPreferencesUtils;
import com.bq.gametrade.utils.ToastUtils;
import com.bq.gametrade.view.CircleImageView;
import com.bumptech.glide.Glide;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class FriendInformationActivity extends BaseActivity implements View.OnClickListener,ChangeNoteDialog.OnChangeNoteListener{

    private ImageView back;
    private TextView title,titleLeftText;
    private CircleImageView friendHeadImage;
    private TextView tvFriendNickName, tvFriendBqAccount, tvFriendAccount;
    private String friendName = "", friendBqAccount = "", friendAccount = "", strFriendHeadImage = "",friendNote="";
    private Button btnTransfer, btnSendRedEnvelope;
    private LinearLayout layEditorNote;
    private UserInfoDetails userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friendinfomration);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        friendName = getIntent().getStringExtra("friendName");
        friendBqAccount = getIntent().getStringExtra("friendBqAccount");
        friendAccount = getIntent().getStringExtra("friendAccount");
        strFriendHeadImage = getIntent().getStringExtra("strFriendHeadImage");
        friendNote= getIntent().getStringExtra("friendNote");
        if (friendBqAccount.equals("") || friendName.equals("") || friendAccount.equals("")) {
            toastShow("获取好友信息出错，请重试");
            finish();
        }

        initView();
        setViewData();

    }

    private void initView() {
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        title = serchViewById(R.id.title_name);
        title.setText("好友信息");
        titleLeftText = serchViewById(R.id.title_leftText);
        titleLeftText.setOnClickListener(this);
        friendHeadImage = serchViewById(R.id.civ_friendHeadImage);
        tvFriendNickName = serchViewById(R.id.tv_friendName);
        tvFriendBqAccount = serchViewById(R.id.tv_friendBqAccount);
        tvFriendAccount = serchViewById(R.id.tv_friendAccount);
        btnTransfer = serchViewById(R.id.btn_transfer);
        btnTransfer.setOnClickListener(this);
        btnSendRedEnvelope = serchViewById(R.id.btn_sendRedEnvelope);
        btnSendRedEnvelope.setOnClickListener(this);
        layEditorNote = serchViewById(R.id.lay_editorNote);
        layEditorNote.setOnClickListener(this);
    }

    private void setViewData() {
        Glide.with(FriendInformationActivity.this)
                .load(strFriendHeadImage)
                .error(R.mipmap.user_headimg_gray)
                .into(friendHeadImage);
        if(!friendNote.equals("")){
            tvFriendNickName.setText(Base64Util.decodeStr(friendNote));
        }else {
            if(!friendName.equals("")){
                tvFriendNickName.setText(Base64Util.decodeStr(friendName));
            }else {
                tvFriendNickName.setText(friendBqAccount);
            }
        }

        tvFriendBqAccount.setText(friendBqAccount);
        tvFriendAccount.setText(friendAccount);
        friendHeadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.title_leftText:
                startActivity(new Intent(FriendInformationActivity.this,RedEnvelopeRecordsActivity.class));
                break;
            case R.id.btn_transfer:
                toIntent(1);
                break;
            case R.id.btn_sendRedEnvelope:
                toIntent(2);
                break;
            case R.id.lay_editorNote:
                ChangeNoteDialog dialog = new ChangeNoteDialog();
                dialog.show(getSupportFragmentManager(),"");
                break;
        }
    }


    @Override
    public void onNote(String text) {
        changeFriendNote(text);
        tvFriendNickName.setText(text);
    }

    /**
     * @param type 1:跳转 转账；2：红包
     */
    private void toIntent(int type) {
        Intent intent = new Intent();
        if (type == 1) {
            intent.setClass(FriendInformationActivity.this, FriendsTransferActivity.class);
        } else if (type == 2) {
            intent.setClass(FriendInformationActivity.this, RedEnvelopeListActivity.class);
        }
        intent.putExtra("friendBqAccount", friendBqAccount);
        intent.putExtra("friendName", friendName);
        intent.putExtra("friendAccount", friendAccount);
        intent.putExtra("strFriendHeadImage", strFriendHeadImage);
        startActivity(intent);
    }

    private void changeFriendNote(String note){
        Map<String,String> params = new HashMap<>();
        params.put("token",userInfo.token);
        params.put("friendId",friendAccount);
        params.put("remark",Base64Util.encode(note.getBytes()));
        showSpotsDialog("正在修改···");
        VRHttp.getResponse(FriendInformationActivity.this, HttpLink.CHANGE_FRIEND_NOTE_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if(response != null){
                    try {
                        JSONObject object = new JSONObject(response.toString());
                        String status = object.getString("status");
                        String message = object.getString("message");
                        if(status.equals("0")){
                            ToastUtils.showToast(FriendInformationActivity.this,Base64Util.decodeStr(message));
                        }else if(status.equals("1")){
                            ToastUtils.showToast(FriendInformationActivity.this,Base64Util.decodeStr(message));
                        }else if (status.equals("99")) {
                            ToastUtils.showToast(FriendInformationActivity.this,"登录超时请重新登陆");
                            application.setUserInfo(null);
                            startActivity(new Intent(FriendInformationActivity.this, LoginActivity.class));
                        } else if (status.equals("98")) {
                            ToastUtils.showToast(FriendInformationActivity.this,"您的账号已在其它设备登录，请重新登录修改您的密码");
                            application.setUserInfo(null);
                            startActivity(new Intent(FriendInformationActivity.this, LoginActivity.class));
                        } else {
                            ToastUtils.showToast(FriendInformationActivity.this,"修改好友备注失败！");
                        }
                    }catch (JSONException e){
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }
}
